package com.ruoyi.user.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.user.mapper.UserMapper;
import com.ruoyi.user.vo.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 🤙🤙🏼🤙🏽
 * @date 2025 4月 18 08:58
 * @Description
 */
@RestController
@RequestMapping("/index/index")
public class IndexController {
    @Resource
    private UserMapper userMapper;


    @PostMapping
    public IndexResponseVo index(@RequestBody IndexVo requestVo) {
        IndexResponseVo orderData = userMapper.selectOrderData(requestVo);
        IndexResponseVo responseVo = new IndexResponseVo();
        if (!CheckEmptyUtil.isEmpty(orderData)) {
            responseVo.setOrderCount(orderData.getOrderCount());
            responseVo.setOrderAmount(orderData.getOrderAmount());
            responseVo.setOrderItemAmount(orderData.getOrderItemAmount());
            responseVo.setOrderCarAmount(orderData.getOrderCarAmount());
            responseVo.setCouponAmount(orderData.getCouponAmount());
        }

        IndexResponseVo userData = userMapper.selectUserData(requestVo);
        if (!CheckEmptyUtil.isEmpty(userData)) {
            responseVo.setUserCount(userData.getUserCount());
        }

        IndexResponseVo blackUserData = userMapper.selectBlackUserData(requestVo);
        if (!CheckEmptyUtil.isEmpty(blackUserData)) {
            responseVo.setBlackUserCount(blackUserData.getBlackUserCount());
        }

        IndexResponseVo bellOrderData = userMapper.selectBellOrderData(requestVo);
        if (!CheckEmptyUtil.isEmpty(bellOrderData)) {
            responseVo.setBellOrderCount(bellOrderData.getBellOrderCount());
            responseVo.setBellOrderAmount(bellOrderData.getBellOrderAmount());
        }

        IndexResponseVo refundOrderData = userMapper.selectRefundOrderData(requestVo);
        if (!CheckEmptyUtil.isEmpty(refundOrderData)) {
            responseVo.setOrderRefundCount(refundOrderData.getOrderRefundCount());
            responseVo.setOrderRefundAmount(refundOrderData.getOrderRefundAmount());
        }

        BigDecimal salesmanIncome = userMapper.selectSalesmanIncome(requestVo);
        BigDecimal channelIncome = userMapper.selectChannelIncome(requestVo);
        BigDecimal brokerIncome = userMapper.selectBrokerIncome(requestVo);
        responseVo.setSalesmanIncome(salesmanIncome);
        responseVo.setChannelIncome(channelIncome);
        responseVo.setBrokerIncome(brokerIncome);
        return responseVo;
    }

    @GetMapping("indexUser")
    public IndexUserBaseResponseVo indexUser() {
        IndexUserBaseResponseVo responseVo = new IndexUserBaseResponseVo();
        List<IndexUserResponseVo> responseVoList = new ArrayList<>();
        Integer count = userMapper.selectCount();
        if (CheckEmptyUtil.isEmpty(count)) {
            count = 0;
        }
        //未消费用户数
        Integer notConsumedCount = userMapper.selectNotConsumedCount();
        if (CheckEmptyUtil.isEmpty(notConsumedCount)) {
            notConsumedCount = 0;
        }
        //消费一回用户
        Integer consumedOneCount = 0;
        List<Long> longs = userMapper.selectConsumedOneCount();
        if (!CheckEmptyUtil.isEmpty(longs)) {
            consumedOneCount = longs.size();
        }
        //黑名单用户
        Integer blackUserCount = userMapper.selectBlackUserCount();
        if (CheckEmptyUtil.isEmpty(blackUserCount)) {
            blackUserCount = 0;
        }
        IndexUserResponseVo notConsumedCountVo = new IndexUserResponseVo();
        notConsumedCountVo.setName("未消费用户");
        notConsumedCountVo.setValue(notConsumedCount);
        if (count == 0) {
            notConsumedCountVo.setRate(BigDecimal.ZERO);
        } else {
            BigDecimal divide = new BigDecimal(notConsumedCount).divide(new BigDecimal(count), 2, RoundingMode.HALF_UP);
            notConsumedCountVo.setRate(divide.multiply(new BigDecimal(100)));
        }
        responseVoList.add(notConsumedCountVo);

        IndexUserResponseVo consumedOneCountVo = new IndexUserResponseVo();
        consumedOneCountVo.setName("消费一次用户");
        consumedOneCountVo.setValue(consumedOneCount);
        if (count == 0) {
            consumedOneCountVo.setRate(BigDecimal.ZERO);
        } else {
            BigDecimal divide = new BigDecimal(consumedOneCount).divide(new BigDecimal(count), 2, RoundingMode.HALF_UP);
            consumedOneCountVo.setRate(divide.multiply(new BigDecimal(100)));
        }
        responseVoList.add(consumedOneCountVo);

        IndexUserResponseVo blackUserCountVo = new IndexUserResponseVo();
        blackUserCountVo.setName("黑名单用户");
        blackUserCountVo.setValue(blackUserCount);
        if (count == 0) {
            blackUserCountVo.setRate(BigDecimal.ZERO);
        } else {
            BigDecimal divide = new BigDecimal(blackUserCount).divide(new BigDecimal(count), 2, RoundingMode.HALF_UP);
            blackUserCountVo.setRate(divide.multiply(new BigDecimal(100)));
        }
        responseVoList.add(blackUserCountVo);
        responseVo.setUserResponseVoList(responseVoList);
        responseVo.setUserCount(count);

        return responseVo;
    }


    @PostMapping("indexUserDate")
    public List<IndexUserDateResponseVo> indexUserDate(@RequestBody IndexVo indexVo) {
        DateTime startTime;
        DateTime endTime;
        if (CheckEmptyUtil.isEmpty(indexVo.getStartTime()) || CheckEmptyUtil.isEmpty(indexVo.getEndTime())) {
            startTime = DateUtil.offsetDay(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -7)), 1);
            endTime = DateUtil.endOfDay(new Date());
        } else {
            startTime = DateUtil.offsetDay(DateUtil.beginOfDay(indexVo.getStartTime()), 1);
            endTime = DateUtil.endOfDay(indexVo.getEndTime());
        }

        return userMapper.indexUser(startTime.toString(), endTime.toString());
    }


    @PostMapping("indexOrderDate")
    public IndexOrderDateBaseResponseVo indexOrderDate(@RequestBody IndexVo indexVo) {
        //IndexOrderDateBaseResponseVo responseVo = new IndexOrderDateBaseResponseVo();
        //List<IndexOrderDateResponseVo> orderDate;
        //if (indexVo.getType() == 3) {
        //    orderDate = userMapper.selectYearOrderDate(indexVo);
        //} else {
        //    orderDate = userMapper.selectOrderDate(indexVo);
        //}
        //responseVo.setOrderDateResponseVo(orderDate);
        //
        //List<IndexOrderDateResponseVo> refundDate ;
        //if (indexVo.getType() == 3) {
        //    refundDate = userMapper.selectYearRefundOrderDate(indexVo);
        //} else {
        //    refundDate = userMapper.selectRefundOrderDate(indexVo);
        //}
        //responseVo.setRefundDateResponseVo(refundDate);
        //
        //return responseVo;

        IndexOrderDateBaseResponseVo responseVo = new IndexOrderDateBaseResponseVo();
        DateTime startTime = DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), 1);
        DateTime endTime = DateUtil.endOfDay(new Date());
        if (indexVo.getType() == 0) {
            startTime = DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), 1);
            endTime = DateUtil.endOfDay(new Date());
        } else if (indexVo.getType() == 1) {
            startTime = DateUtil.offsetDay(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -7)), 1);
            endTime = DateUtil.endOfDay(new Date());
        } else if (indexVo.getType() == 2) {
            startTime = DateUtil.offsetDay(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -30)), 1);
            endTime = DateUtil.endOfDay(new Date());
        } else if (indexVo.getType() == 3) {
            startTime = DateUtil.offsetDay(DateUtil.beginOfYear(new Date()), 1);
            endTime = DateUtil.endOfDay(new Date());
        } else if (indexVo.getType() == 4) {
            startTime = DateUtil.offsetDay(DateUtil.beginOfDay(indexVo.getStartTime()), 1);
            endTime = DateUtil.endOfDay(indexVo.getEndTime());
        }
        responseVo.setOrderDateResponseVo(userMapper.test(startTime.toString(), endTime.toString()));
        responseVo.setRefundDateResponseVo(userMapper.selectRefundOrder(startTime.toString(), endTime.toString()));

        return responseVo;
    }


    public static void main(String[] args) {
        System.out.println(DateUtil.offsetDay(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), -7)), 1));
    }
}


